﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using IsmlTools.Common;
using IsmlTools.Dal.SQL;
using System.Data.Entity;

namespace IsmlTools.Common
{
    public class NameHelper
    {
        public Dictionary<string, string> GetSeasonName(ISMLEntities db, int seasonID)
        {
            var query = db.SeasonNames.Where(x => x.SeasonID == seasonID);

            return query.ToDictionary(seasonname => seasonname.NameCode, seasonname => seasonname.English);

        }

        public Dictionary<int, string> GetSeasonName(string url, string language)
        {
            Dictionary<int, string> name = new Dictionary<int, string>();
            RegHelper reghelper = new RegHelper();
            WebHelper webhelper = new WebHelper();


            name.Clear();
            string page = webhelper.ReadPage(url);

            //languageCode = language;
            page = page.Replace("\")\n}", "\"###\n}");
            page = page.Replace("\")\r\n}", "\"###\r\n}");
            page = page.Replace("),", "###");
            page = page.Replace("\"", "");
            page = page.Replace("フィン・エ・ルド・スイ・ラフィンティ, ラン", "フィン・エ・ルド・スイ・ラフィンティ(ラン)");


            string s = reghelper.GetMidPattern(language + @" : Array\(", "###");
            Regex reg = new Regex(reghelper.GetMidPattern(language + @"[ ]+?: Array\(", "###"));
            Regex reg1 = new Regex(reghelper.GetMidPattern("var nList", @" \= \{"));

            MatchCollection mc = reg.Matches(page);
            MatchCollection mc1 = reg1.Matches(page);

            for (int i = 0; i < mc.Count; i++)
            {
                if (!mc[i].Value.Equals("{}") && !mc[i].Value.Equals(string.Empty))
                {
                    string[] lines = Regex.Split(mc[i].Value, ",");
                    int num = 0;
                    num = i < mc.Count - 2 ? int.Parse(mc1[i / 2].Value) + 1 : 1;

                    for (int j = 0; j < lines.Length; j++)
                    {
                        if (!name.ContainsKey(num + j))
                            name.Add(num + j, Regex.Replace(lines[j], @"[‧•・]", "·"));
                    }
                }
                i++;
            }

            return name;
        }

        public void SaveSeasonName(string url)
        {
            //if (languageCode == null) throw new ArgumentNullException("languageCode");

            Dictionary<int, string> name = new Dictionary<int, string>();
            RegHelper reghelper = new RegHelper();
            WebHelper webhelper = new WebHelper();

            //string page = webhelper.ReadPage(WebPageLink.NamesSeason);



            foreach (string language in LanguageCode.AllLanguage)
            {
                name = GetSeasonName(url, language);

                try
                {
                    using (ISMLEntities db = new ISMLEntities())
                    {
                        foreach (var v in name)
                        {
                            string temp = v.Key.ToString(CultureInfo.InvariantCulture);
                            var query = db.SeasonNames.FirstOrDefault(x => x.SeasonID == 5 && x.NameCode == temp);
                            if (query == null)
                            {
                                SeasonName seasonname = new SeasonName { SeasonID = 5, NameCode = v.Key.ToString() };
                                switch (language)
                                {
                                    case LanguageCode.Sc:
                                        seasonname.SimplifiedChinese = v.Value;
                                        break;
                                    case LanguageCode.En:
                                        seasonname.English = v.Value;
                                        break;
                                    case LanguageCode.Jp:
                                        seasonname.Japanese = v.Value;
                                        break;
                                    case LanguageCode.Kr:
                                        seasonname.Korean = v.Value;
                                        break;
                                    case LanguageCode.Ru:
                                        seasonname.Russian = v.Value;
                                        break;
                                    case LanguageCode.Tc:
                                        seasonname.TraditionalChinese = v.Value;
                                        break;
                                }
                                db.SeasonNames.Add(seasonname);
                            }
                            else
                            {
                                switch (language)
                                {
                                    case LanguageCode.Sc:
                                        query.SimplifiedChinese = v.Value;
                                        break;
                                    case LanguageCode.En:
                                        query.English = v.Value;
                                        break;
                                    case LanguageCode.Jp:
                                        query.Japanese = v.Value;
                                        break;
                                    case LanguageCode.Kr:
                                        query.Korean = v.Value;
                                        break;
                                    case LanguageCode.Ru:
                                        query.Russian = v.Value;
                                        break;
                                    case LanguageCode.Tc:
                                        query.TraditionalChinese = v.Value;
                                        break;
                                }
                            }
                        }
                        db.SaveChanges();
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }
    }
}
